package andy_project.gson;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.IOException;
import java.io.InputStream;


public class MainActivity extends ActionBarActivity {
    private Handler handler;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView= (TextView) findViewById(R.id.textView);
        handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                String content= (String) msg.obj;
                textView.setText(content);
            }
        };
        new Thread(new Runnable() {
            @Override
            public void run() {
                String url="http://192.168.1.101:8080/person.json";
                HttpClient httpClient=new DefaultHttpClient();
                HttpGet httpGet=new HttpGet(url);
                try {
                   HttpResponse response= httpClient.execute(httpGet);
                    if (response.getStatusLine().getStatusCode()==200){
                        HttpEntity httpEntity=response.getEntity();
                        if (httpEntity!=null) {
                            InputStream instream = httpEntity.getContent();
                            String content="";
                            while(true){
                                           byte[] bytes=new byte[1024*1000];
                                           int k=instream.read(bytes);
                                           if(k>=0)content=content+new String(bytes,0,k);
                                           else break;
                                           System.out.println(content);
                                           System.out.println("=========================================================================================");

                                           Message message=new Message();
                                           message.arg1=1;
                                           message.obj=content;
                                           handler.sendMessage(message);
                                         }

                                 }

                        }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    class Person{
        private int id;
        private String name;
        private String adress;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAdress() {
            return adress;
        }

        public void setAdress(String adress) {
            this.adress = adress;
        }
    }


    class GsonTools{
        

    }

}
